package com.kaili.controller.pda.v1;

import com.kaili.db.service.IBaseCarService;
import com.kaili.db.service.IHsEwbsStowageService;
import com.kaili.db.service.IHsEwbsStowageSubService;
import com.kaili.db.service.IHsOptArriveService;
import com.kaili.pojo.dto.arrive.ArriveStowageDTO;
import com.kaili.pojo.entity.BaseCar;
import com.kaili.pojo.entity.HsEwbsStowageSub;
import com.kaili.pojo.vo.StowageInfoVO;
import com.kaili.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @AUTHOR pengdongxu.
 * @Date: 18-5-14 上午9:13.
 * @COPYRIGHT 杭州凯立
 */

@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "[pda]到件服务", description = "[pda]到件服务API")
@RestController("arriveBasicControllerP")
public class ArriveBasicController {
    @Autowired
    private IHsOptArriveService hsOptArriveService;
    @Autowired
    private IHsEwbsStowageService stowageService;
    @Autowired
    private IBaseCarService carService;
    @Autowired
    private IHsEwbsStowageSubService hsEwbSubService;

    @ApiOperation("[PDA]子单到件")
    @PostMapping("/p/arrives/bat")
    public List<String> doArriveSub(@RequestBody List<String> arriveSub){
        HsEwbsStowageSub hsEwbSub ;
        if (arriveSub == null || arriveSub.size()==0){
            return null;
        }
        List<String> list = new ArrayList<>();
        Set<String> set = new HashSet<>();

        for (String sub :arriveSub) {
            hsEwbSub = hsEwbSubService.selectStowageSub(sub);
            if (hsEwbSub == null){
                list.add(sub);
            }else {
                set.add(hsEwbSub.getOperationId());
            }
        }

        if (!set.isEmpty()){
            List<ArriveStowageDTO> stowageDTOS = new ArrayList<>();
            BaseCar baseCar;
            for (String s : set) {
                ArriveStowageDTO stowageDTO = new ArriveStowageDTO();
                stowageDTO.setOperationId(s);
                stowageDTO.setSourceType(2);
                stowageDTOS.add(stowageDTO);
                hsOptArriveService.doStowageArriveConfirm(stowageDTOS);

                Map<String,Object> searchMap = new HashMap<>();
                searchMap.put("operationId", s);
                StowageInfoVO stowageInfo = stowageService.queryStowageInfo(searchMap);
                baseCar = new BaseCar();
                baseCar.setId(stowageInfo.getCarId());
                baseCar.setCarStatus("空闲");
                carService.updateById(baseCar);
            }

        }
        return list;
    }
}
